#ifndef __UDSF_RADAR_DACQ_C__
#define __UDSF_RADAR_DACQ_C__

#include "app.h"
/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/

#include "udsf_uart.c"
#include "udsf_spi.c"
#include "udsf_delay.c"
#include "udsf_rtc.c"
#include "udsf_protocol_debug.c"

#if defined(USE_EXRF_RC6011) || defined(USE_EXRF_RC6011C) || defined(USE_EXRF_RC6011AIP)
#include "udsf_rc6011_c_aip.c"

static int udsf_radar_ex_dacq_continue(void) __attribute__((unused));

static int udsf_radar_ex_dacq_continue(void)
{
	int res = 0;

	do{
		if((res = udsf_rc6011_init()) < 0){
			break;
		}
		if((res = udsf_rc6011_ana_config()) < 0){
			break;
		}
		if((res = udsf_rc6011_ramp_wave_config()) < 0){
			break;
		}

		/* adc and prep init*/
		
		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);// 至少4us后发送wave_start才能正确捕获sync信号 why?

		if((res = udsf_rc6011_ramp_mode_config()) < 0){
			break;
		}
		BBE_PREPINIT(paramDataPrep);
		/* ramp start */
		udsf_rc6011_wave_start();
		bbe_regb_str->ISR = BBE_ISR_RAMP_PEDGE;
		while( (bbe_regb_str->ISR & BBE_ISR_RAMP_PEDGE)==0 );// wair for wave0-B
		udsf_rc6011_ana_cfg_rf_if_on();
		/* wait dacq */
		BBE_OPCLEAR(BBE_CLEAR_PREP);
		BBE_ISR_CLEAR(BBE_ISR_PREP_FRAME_END);		
		BBE_WAITISR(BBE_ISR_PREP_FRAME_END);  // wait prep

		//udsf_rc6011_ana_cfg_rf_if_off();
		//udsf_rc6011_wave_stop();
		udsf_rc6011_deinit();
		ANACFG_SET(&paramANA_Venus->ana1);
		
		BBE_OPGATE_DIS(BBE_GATE_PREP);        // prep deinit
		BBE_OPCLEAR(BBE_CLEAR_PREP);
		return res;
	}while(0);
	
	/* ex-rf: chip disable, spi deinit */
	udsf_rc6011_deinit();
	
	return res;
}

static int udsf_radar_ex_dacq_chirpsum(int predata_en,int alg0_en,int alg25_en){

	#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
	GPIO_OUTPUT_SET = 0;
	#endif
	if( predata_en ){
		PREPDATA_HANDLE();
	}
	#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
	GPIO_OUTPUT_SET = 1;
	udsf_delay_us(100);
	#endif

	#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
	GPIO_OUTPUT_SET = 0;
	#endif
	int res = 0;

	do{
		if((res = udsf_rc6011_init()) < 0){
			break;
		}

		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 1;
		#endif
		if((res = udsf_rc6011_ana_config()) < 0){
			break;
		}		
		if((res = udsf_rc6011_ramp_wave_config()) < 0){
			break;
		}
		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 0;
		#endif

		/* adc and prep init*/
		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);// 至少4us后发送wave_start才能正确捕获sync信号 why?
		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 1;
		#endif

		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 0;
		#endif
		if((res = udsf_rc6011_ramp_mode_config()) < 0){
			break;
		}
		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 1;
		#endif

		BBE_PREPINIT(paramDataPrep);
		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 0;
		#endif

		udsf_rc6011_wave_start();
		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 1;
		#endif

		bbe_regb_str->ISR = BBE_ISR_RAMP_PEDGE;     
		while( (bbe_regb_str->ISR & BBE_ISR_RAMP_PEDGE)==0 ); // wair for wave0-B

		// 开启RF
		int chirp_cnt = 0;
		do{
			#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
			GPIO_OUTPUT_SET = 0;
			#endif

			udsf_rc6011_ana_cfg_rf_if_on(); // open "RF、LO、PLL、IF"

			#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
			GPIO_OUTPUT_SET = 1;
			#endif

			/* wait dacq */
			BBE_OPCLEAR(BBE_CLEAR_PREP);
			BBE_ISR_CLEAR(BBE_ISR_PREP_FRAME_END);
			BBE_WAITISR(BBE_ISR_PREP_FRAME_END);      // 等待采集结束
			bbe_regb_str->ISR = BBE_ISR_RAMP_PEDGE;
			#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
			GPIO_OUTPUT_SET = 0;
			#endif
			chirp_cnt++;
			if( paramDataPrep->cycleNum != chirp_cnt ){
				udsf_rc6011_ana_cfg_rf_if_off(); // close "RF、LO、PLL、IF"
				if( alg0_en ){
					P2_FFT_STEPCFG8_HANDLE((P2_FFT_CONFIG*)&paramALG->alg0,paramALG->alg0.dstInc*(chirp_cnt-1));
				}
				if( alg25_en ){
					P2_FFT_STEPCFG8_HANDLE((P2_FFT_CONFIG*)&paramALG->alg25,paramALG->alg25.dstInc*(chirp_cnt-1));
				}
			}

			#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
			GPIO_OUTPUT_SET = 1;
			#endif

			if( paramDataPrep->cycleNum == chirp_cnt ){
				#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
				GPIO_OUTPUT_SET = 0;
				#endif
				udsf_rc6011_deinit();
				#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
				GPIO_OUTPUT_SET = 1;
				#endif

				ANACFG_SET(&paramANA_Venus->ana1);// only 40M
				#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
				GPIO_OUTPUT_SET = 0;
				#endif

				BBE_OPGATE_DIS(BBE_GATE_PREP);    // prep deinit
				BBE_OPCLEAR(BBE_CLEAR_PREP);		
				#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
				GPIO_OUTPUT_SET = 1;
				#endif

				if( alg0_en ){
					P2_FFT_STEPCFG8_HANDLE((P2_FFT_CONFIG*)&paramALG->alg0,paramALG->alg0.dstInc*(chirp_cnt-1));
				}

				if( alg25_en ){
					P2_FFT_STEPCFG8_HANDLE((P2_FFT_CONFIG*)&paramALG->alg25,paramALG->alg25.dstInc*(chirp_cnt-1));
				}
				
				#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
				GPIO_OUTPUT_SET = 0;
				#endif
				break;
			}else{
				while( (bbe_regb_str->ISR & BBE_ISR_RAMP_PEDGE)==0 ); // 等待ramp结束
				udsf_rc6011_wave_start();                             // 开始新的ramp
				bbe_regb_str->ISR = BBE_ISR_RAMP_PEDGE;
				while( (bbe_regb_str->ISR & BBE_ISR_RAMP_PEDGE)==0 ); // 等待新ramp的wave0-B
				
				#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
				GPIO_OUTPUT_SET = 0;
				#endif
			}
		}while(1);

		//udsf_rc6011_wave_stop();
		#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
		GPIO_OUTPUT_SET = 1;
		#endif
		return res;

	}while(0);
	
	udsf_rc6011_deinit();
	#if (TEST_MODE == 40) && defined( GPIO_OUTPUT_ENABLE )
	GPIO_OUTPUT_SET = 1;
	#endif
	return res;
}

#if TEST_MODE == 30
static int udsf_radar_ex_dacq_test_current(int step){

	BBE_OPGATE_DIS(BBE_GATE_PREP|BBE_GATE_FFT|BBE_GATE_P2|BBE_GATE_CFAR);

	ANACFG_SET(&paramANA_Venus->ana1);

	if( step == 0 ){
		BBE_OPGATE_DIS(BBE_GATE_PREP|BBE_GATE_FFT|BBE_GATE_P2|BBE_GATE_CFAR);
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 1 ){
		BBE_PREPINIT(paramDataPrep);
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 2 ){
		
		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		
		while(1){
			WDT->STR = 1;
		}
	}
	
	if( step == 3 ){
		BBE_PREPINIT(paramDataPrep);
		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		
		while(1){
			WDT->STR = 1;
		}
	}
	
//	if( step == 200 ){ // 查看ANA默认值
//		udsf_rc6011_init();
//		uint16_t read_reg_data;
//		while(1){

//			read_reg_data = udsf_ex_rf_reg_read(RC6011_SYS_STA0_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"sta:","\n");
//			
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG8_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 8:","\n");
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG18_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 18:","\n");
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG22_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 22:","\n");

////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG0_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 0:","\n");
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG1_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 1:","\n");
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG2_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 2:","\n");
////			
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG9_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 9:","\n");
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG10_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 10:","\n");
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG19_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 19:","\n");
////			
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG20_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 20:","\n");
////			read_reg_data = udsf_ex_rf_reg_read(RC6011_ANA_CFG21_ADDR);
////			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ana cfg 21:","\n");
//			udsf_delay_ms(500);
//		}
//	}
//	
//	if( step == 201 ){ // 查看RAMP mode默认值
//		udsf_rc6011_init();
//		uint16_t read_reg_data;
//		while(1){

//			read_reg_data = udsf_ex_rf_reg_read(RC6011_SYS_STA0_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"sta:","\n");
//			
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_PLL_CFG0_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp pll cfg0:","\n");
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_PLL_CFG1_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp pll cfg1:","\n");			

//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_SYNCW_CFG0_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp syncw cfg0:","\n");
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_SYNCW_CFG1_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp syncw cfg1:","\n");
//			
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_ACCW_CFG0_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp accw cfg0:","\n");
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_ACCW_CFG1_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp accw cfg1:","\n");
//			
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_WM_CFG0_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp wm cfg0:","\n");
//			read_reg_data = udsf_ex_rf_reg_read(RC6011_RAMP_WM_CFG1_ADDR);
//			udsf_uart_send_u16_base16(1,&read_reg_data,1,"ramp wm cfg1:","\n");
//			
//			udsf_delay_ms(500);
//		}
//	}
	
//	if( step == 202 ){ // 查看WAVE MEM默认值
//		udsf_rc6011_init();
//		uint16_t read_reg_data;
//		udsf_rc6011_wave_clear();
//		while(1){

//			for( int i=0;i<3;i++){
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_7);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col7:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_6);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col6:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_5);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col5:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_4);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col4:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_3);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col3:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_2);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col2:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_1);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col1:","\n");
//				read_reg_data = udsf_ex_rf_wavemem_read(i,WAVEMEM_COL_ADDR_0);
//				udsf_uart_send_u16_base16(1,&read_reg_data,1,"col0:","\n");				
//			}
//			
//			udsf_delay_ms(500);
//		}
//	}
	
	if( step == 100 ){
		
		udsf_rc6011_init();
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 101 ){
			/* ana cfg */
		udsf_rc6011_init();
		udsf_rc6011_ana_config();

		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 102 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();
		while(1){
			WDT->STR = 1;
		}
	}
	
	if( step == 103 ){
		udsf_rc6011_init();
		udsf_rc6011_ramp_wave_config();
		
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 104 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();

		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);

		while(1){
			WDT->STR = 1;
		}
	}
	
	if( step == 105 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();

		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		udsf_rc6011_ramp_mode_config();

		while(1){
			WDT->STR = 1;
		}
	}
	
	if( step == 106 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();

		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		udsf_rc6011_ramp_mode_config();
		BBE_PREPINIT(paramDataPrep);
		
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 107 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();

		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		udsf_rc6011_ramp_mode_config();
		BBE_PREPINIT(paramDataPrep);
		udsf_rc6011_wave_start();
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 108 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();

		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		udsf_rc6011_ramp_mode_config();
		BBE_PREPINIT(paramDataPrep);
		udsf_rc6011_wave_start();

		udsf_rc6011_ana_cfg_rf_if_on();
		while(1){
			WDT->STR = 1;
		}
	}

	if( step == 109 ){
		udsf_rc6011_init();
		udsf_rc6011_ana_config();
		udsf_rc6011_ramp_wave_config();

		ANACFG ana2 = paramANA_Venus->ana1;
		ana2.ana_cfg0 |= 0x2000030C;
		ANACFG_SET(&ana2);
		udsf_rc6011_ramp_mode_config();
		BBE_PREPINIT(paramDataPrep);
		//udsf_rc6011_wave_start();

		udsf_rc6011_ana_cfg_rf_if_on();
		while(1){
			WDT->STR = 1;
		}
	}


	if( step == 112 ){
		/* ana cfg */
		int res;
		int error = 0;
		
		if( (res = udsf_rc6011_init()) < 0 ){
			error = res;
		}
		else if( (res = udsf_rc6011_ana_config()) < 0 ){
			error = res;
		}
		else if( (res = udsf_rc6011_ramp_wave_config()) < 0 ){
			error = res;
		}

		if( res < 0){
			;
		}else{
			
			ANACFG ana2 = paramANA_Venus->ana1;
			ana2.ana_cfg0 |= 0x2000030C;
			ANACFG_SET(&ana2);
			
			if( (res = udsf_rc6011_ramp_mode_config()) < 0 ){
				error = res;
			}
			
			BBE_PREPINIT(paramDataPrep);
		}
		
		if( error >= 0 ){
			udsf_uart_send_string("succ 112\n");
			udsf_rc6011_wave_start();
		}
		
		while(1){
			WDT->STR = 1;
			if( error < 0 ){
				udsf_delay_ms(200);
				udsf_uart_send_string("aaa\n");
			}
		}
	}
	
	if( step == 113 ){
		/* ana cfg */
		int res;
		int error = 0;
		
		if( (res = udsf_rc6011_init()) < 0 ){
			error = res;
		}
		else if( (res = udsf_rc6011_ramp_wave_config()) < 0 ){
			error = res;
		}
		else if( (res = udsf_rc6011_ana_config()) < 0 ){
			error = res;
		}
		if( res < 0){
			;
		}else{
			ANACFG ana2 = paramANA_Venus->ana1;
			ana2.ana_cfg0 |= 0x2000030C;
			ANACFG_SET(&ana2);
			
			BBE_PREPINIT(paramDataPrep);
			
			if( (res = udsf_rc6011_ramp_mode_config()) < 0 ){
				error = res;
			}
			else if( (res = udsf_rc6011_wave_start()) < 0 ){
				error = res;
			}
			else if( (res = udsf_rc6011_ana_cfg_rf_if_on()) < 0 ){
				error = res;
			}
		}

		if( error >= 0 ){
			udsf_uart_send_string("succ 113\n");
		}

		while(1){
			WDT->STR = 1;
			if( error < 0 ){
				udsf_delay_ms(200);
				udsf_uart_send_string("aaa\n");
			}
		}
	}

	return 0;
}
#endif

#endif

#endif
